package cn.lb.login.controller;

import cn.lb.constant.LoginConstant;
import cn.lb.login.dto.LoginDTO;
import cn.lb.login.service.ILoginService;
import cn.lb.basic.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController {
    @Autowired
    private ILoginService loginService;
    @PostMapping("/login")
    public R login(@RequestBody LoginDTO dto){
        try {
            return loginService.login(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("登录失败"+e.getMessage());
        }
    }
    @PutMapping("/logout")
    public R logout(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            LoginConstant.LoginUserMap.remove(token);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("登录失败"+e.getMessage());
        }
    }
}
